﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text.RegularExpressions;

    [ScriptType("SP.ContentTypeCollection", ServerTypeId="{653e9412-6245-4a3f-829b-cfdbf7fd86a5}")]
    public class ContentTypeCollection : ClientObjectCollection<ContentType>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public ContentTypeCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public ContentType Add(ContentTypeCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient)
            {
                if (parameters == null)
                {
                    throw ClientUtility.CreateArgumentNullException("parameters");
                }
                if (parameters != null)
                {
                    if (parameters.Name == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.Name");
                    }
                    if ((parameters.Group != null) && (parameters.Group.Length < 1))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Group");
                    }
                    if ((parameters.Group != null) && (parameters.Group.Length > 0x80))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Group");
                    }
                }
            }
            ContentType type = new ContentType(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            type.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(type.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, type);
            base.Context.AddQuery(query);
            base.AddChild(type);
            type.InitFromCreationInformation(parameters);
            return type;
        }

        [Remote]
        public ContentType AddExistingContentType(ContentType contentType)
        {
            if (base.Context.ValidateOnClient && (contentType == null))
            {
                throw ClientUtility.CreateArgumentNullException("contentType");
            }
            ContentType type = new ContentType(base.Context, new ObjectPathMethod(base.Context, base.Path, "AddExistingContentType", new object[] { contentType }));
            type.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(type.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, type);
            base.Context.AddQuery(query);
            base.AddChild(type);
            return type;
        }

        [Remote]
        public ContentType GetById(string contentTypeId)
        {
            Dictionary<string, ContentType> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient)
            {
                if (contentTypeId == null)
                {
                    throw ClientUtility.CreateArgumentNullException("contentTypeId");
                }
                if ((contentTypeId != null) && !Regex.Match(contentTypeId, "0x(([1-9a-fA-F][0-9a-fA-F])|(0[1-9a-fA-F]))*(00([0-9a-fA-F][0-9a-fA-F]){16})*").Success)
                {
                    throw ClientUtility.CreateArgumentException("contentTypeId");
                }
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetById", out obj2))
            {
                dictionary = (Dictionary<string, ContentType>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, ContentType>();
                base.ObjectData.MethodReturnObjects["GetById"] = dictionary;
            }
            ContentType type = null;
            if (!dictionary.TryGetValue(contentTypeId, out type))
            {
                type = new ContentType(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { contentTypeId }));
                dictionary[contentTypeId] = type;
            }
            return type;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

